import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import '../public.dart';
import './account/_index.dart';

/// 我的页面
class MyAccount extends StatefulWidget {
  MyAccount({Key key}) : super(key: key);

  @override
  _MyAccountState createState() {
    return _MyAccountState();
  }
}

class _MyAccountState extends State<MyAccount>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  ScrollController _scrollController;
  UserProvider user;
  UserAccountProvider userAccount;
  ThemeProvider theme;
  @override
  void initState() {
    _tabController = TabController(
        initialIndex: 0, // 起始位置
        length: 3,
        vsync: this);
    _scrollController = ScrollController();
    super.initState();
    providerHanddle();
  }

  void providerHanddle() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      userAccount.setUserAccount();
    });
  }

  @override
  Widget build(BuildContext context) {
    // 使用Provider
    user = Provider.of<UserProvider>(context);
    userAccount = Provider.of<UserAccountProvider>(context);
    theme = Provider.of<ThemeProvider>(context);
    return Scaffold(
        backgroundColor: theme.primaryColor,
        appBar: AppBar(
          title: Text('我的账户'),
          leadingWidth: 44,
          leading: BackComponent(),
          backgroundColor: theme.primaryColor,
          centerTitle: true,
          actions: <Widget>[],
        ),
        body: NestedScrollView(
            floatHeaderSlivers: false,
            // clipBehavior: Clip.antiAliasWithSaveLayer,
            // physics: ClampingScrollPhysics(),
            controller: _scrollController,
            headerSliverBuilder:
                (BuildContext context, bool innerBoxIsScrolled) {
              return <Widget>[
                SliverToBoxAdapter(
                    child: Container(
                        padding: EdgeInsets.only(left: 16, right: 16),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text('我的积分',
                                style: TextStyle(
                                    color: ColorUtil.rgba(153, 153, 153, 1),
                                    fontSize: 12)),
                            Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: <Widget>[
                                  Row(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.end,
                                      children: [
                                        Text('${userAccount.data.score}',
                                            style: TextStyle(
                                                color: Colors.white,
                                                height: 1.4,
                                                fontSize: 40)),
                                        userAccount.data.score > 100
                                            ? Container(
                                                height: 18,
                                                margin: EdgeInsets.only(
                                                    bottom: 10, left: 8),
                                                padding: EdgeInsets.fromLTRB(
                                                    6, 3, 6, 3),
                                                decoration: BoxDecoration(
                                                  border: Border.all(
                                                    width: 1,
                                                    color: ColorUtil.rgba(
                                                        254, 45, 102, 1),
                                                  ),
                                                  borderRadius:
                                                      BorderRadius.circular(4),
                                                ),
                                                child: Text(
                                                  '约${userAccount.data.scoreExchange}元',
                                                  style: TextStyle(
                                                    fontSize: 10,
                                                    height: 1,
                                                    color: ColorUtil.rgba(
                                                        254, 45, 102, 1),
                                                  ),
                                                ))
                                            : SizedBox.shrink()
                                      ]),
                                  Container(
                                      margin: EdgeInsets.only(bottom: 10),
                                      child: Text('10000积分=1元',
                                          style: TextStyle(
                                              color: ColorUtil.rgba(
                                                  153, 153, 153, 1),
                                              fontSize: 12))),
                                ]),
                            Divider(height: 24, color: Colors.transparent),
                            Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Column(children: [
                                    Text('${userAccount.data.scoreToday}',
                                        style: TextStyle(
                                            color: ColorUtil.rgba(
                                                255, 255, 255, 1),
                                            height: 1.25,
                                            fontSize: 16)),
                                    Text('今日积分',
                                        style: TextStyle(
                                            color: ColorUtil.rgba(
                                                255, 255, 255, 0.6),
                                            fontSize: 12)),
                                  ]),
                                  Container(
                                    height: 24,
                                    width: 1,
                                    color: ColorUtil.rgba(255, 255, 255, 0.05),
                                  ),
                                  Column(children: [
                                    Text('${userAccount.data.scoreUnit}',
                                        style: TextStyle(
                                            color: ColorUtil.rgba(
                                                255, 255, 255, 1),
                                            height: 1.25,
                                            fontSize: 16)),
                                    Text('累计积分',
                                        style: TextStyle(
                                            color: ColorUtil.rgba(
                                                255, 255, 255, 0.6),
                                            fontSize: 12)),
                                  ]),
                                  Container(
                                    height: 24,
                                    width: 1,
                                    color: ColorUtil.rgba(255, 255, 255, 0.05),
                                  ),
                                  Column(children: [
                                    Text('${userAccount.data.limit}',
                                        style: TextStyle(
                                            color: ColorUtil.rgba(
                                                255, 255, 255, 1),
                                            height: 1.25,
                                            fontSize: 16)),
                                    Row(
                                      children: [
                                        Text('提现额度(元)',
                                            style: TextStyle(
                                                color: ColorUtil.rgba(
                                                    255, 255, 255, 0.6),
                                                fontSize: 12)),
                                        GestureDetector(
                                            onTap: _quotaTips,
                                            child: Padding(
                                                padding: EdgeInsets.only(
                                                    top: 3, left: 2),
                                                child: SVG.icon('help',
                                                    color: Color.fromRGBO(
                                                        255, 255, 255, 125),
                                                    size: 12)))
                                      ],
                                    )
                                  ])
                                ]),
                            Divider(height: 32, color: Colors.transparent),
                            SizedBox(
                                width: double.infinity,
                                height: 48,
                                child: FlatButton(
                                  textColor: ColorUtil.rgba(51, 51, 51, 1),
                                  color: ColorUtil.rgba(255, 215, 59, 1),
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(24),
                                  ),
                                  onPressed: () {
                                    ACGN.toast.dialog(context,
                                        constraints:
                                            BoxConstraints(maxWidth: 280),
                                        content: Text(
                                          '项目开发中，敬请期待~',
                                          style: TextStyle(color: Colors.white),
                                        ));
                                    // Routers.navigateTo(RouterPage.MY_ACCOUNT);
                                  },
                                  child: Text(
                                    '立即提现',
                                    textAlign: TextAlign.center,
                                    style: TextStyle(fontSize: 18),
                                  ),
                                )),
                            Divider(height: 32, color: Colors.transparent),
                          ],
                        )))
              ];
            },
            body: Container(
                child: Column(children: [
              Container(
                  alignment: Alignment.centerLeft,
                  child: Padding(
                      padding: EdgeInsets.only(left: 16, right: 16),
                      child: TabBar(
                          controller: _tabController,
                          indicatorWeight: 8,
                          // labelColor: ColorUtil.rgba(255, 215, 59, 1),
                          isScrollable: true, // 只有设置滚动才能左对齐
                          labelPadding: EdgeInsets.only(right: 24),
                          labelStyle: TextStyle(
                              color: Colors.red, fontSize: 16, height: 1.75),
                          indicator: CustomUnderlineTabIndicator(
                              width: 28,
                              borderSide: BorderSide(
                                  width: 4,
                                  color: ColorUtil.rgba(255, 215, 59, 1))),
                          onTap: (index) {
                            print('$index');
                          },
                          tabs: [Text('积分明细'), Text('提现明细'), Text('额度明细')]))),
              Expanded(
                  child: TabBarView(
                      // physics: NeverScrollableScrollPhysics(),
                      dragStartBehavior: DragStartBehavior.start,
                      controller: _tabController,
                      children: [
                    CreditPage(),
                    WithdrawalPage(),
                    QuotaPage(),
                  ]))
            ]))));
  }

  _quotaTips() {
    ACGN.toast.dialog(context,
        constraints: BoxConstraints(maxWidth: 280),
        actions: [
          Expanded(
              child: FlatButton(
            height: 48,
            color: Colors.transparent,
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(0)),
            child: Text(
              '知道了',
              style: TextStyle(color: Colors.white, fontSize: 16),
            ),
            onPressed: () {
              Navigator.of(context).pop();
            },
          )),
        ],
        title: '额度说明',
        content:
            Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Padding(
              padding: EdgeInsets.only(bottom: 4),
              child: Text('提现额度是什么？',
                  style: TextStyle(color: Colors.white, fontSize: 16))),
          Text('在提现时，需消耗一定量的提现额度，在提现额度不足时，无法提现',
              style: TextStyle(color: Colors.white, fontSize: 12)),
          Padding(
              padding: EdgeInsets.only(bottom: 4, top: 8),
              child: Text('如何获得提现额度？',
                  style: TextStyle(color: Colors.white, fontSize: 16))),
          Text('1、邀请好友可获得额度红包，1位最高可获得30元',
              style: TextStyle(color: Colors.white, fontSize: 12)),
          Text('2、新用户可获赠随机额度红包，最高可获得10元提现额度',
              style: TextStyle(color: Colors.white, fontSize: 12))
        ]));
  }

  @override
  void dispose() {
    _tabController.dispose();
    _scrollController.dispose();
    super.dispose();
  }
}
